﻿using System.Text;

namespace NetCore8Demo.Tool
{
    public class Log
    {
        private static string _logPath = AppDomain.CurrentDomain.BaseDirectory + "\\LOG\\";
        private static object _lock = new object();
        public static void WriteLog(string text)
        {
            if (!Directory.Exists(_logPath))
            {
                Directory.CreateDirectory(_logPath);
            }
            string fileFullFileName = _logPath + DateTime.Now.ToString("yyyyMMdd") + ".Log";
            lock (_lock)
            {
                StreamWriter sw;
                using (sw = File.AppendText(fileFullFileName))
                {
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:  ") + text);
                }
            }
        }

        public static void WriteLog(string path,string text,bool needTime = true)
        {
            string realPath = _logPath + "\\" + path + "\\";
            if (!Directory.Exists(realPath))
            {
                Directory.CreateDirectory(realPath);
            }
            string fileFullFileName = realPath + DateTime.Now.ToString("yyyyMMdd") + ".Log";
            lock (_lock)
            {
                StreamWriter sw;
                using (sw = File.AppendText(fileFullFileName))
                {
                    if (needTime)
                    {
                        sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ff  ") + text);
                    }
                    else
                    {
                        sw.WriteLine(text);
                    }
                  
                }
            }
        }


        public static void WriteLogWithFileName(string fileName, string text, bool needTime = true)
        {
           
            string fileFullFileName = fileName;
            lock (_lock)
            {
                StreamWriter sw;
                using (sw = File.AppendText(fileFullFileName))
                {
                    if (needTime)
                    {
                        sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ff  ") + text);
                    }
                    else
                    {
                        sw.WriteLine(text);
                    }

                }
            }
        }

        public static void WriteCsv(string text)
        {
            StreamWriter sw;
            if (!Directory.Exists(_logPath))
            {
                Directory.CreateDirectory(_logPath);
            }
            string fileFullFileName = _logPath + DateTime.Now.ToString("yyyyMMdd") + ".CSV";

            if (!File.Exists(fileFullFileName))
            {
               
                lock (_lock)
                {
                    using (sw = new StreamWriter(fileFullFileName, true, Encoding.Default))
                    {
                        sw.WriteLine("二维码,点位名称,标准值,检测结果,是否合格,是否复检,时间");
                    }
                }
            }
            lock (_lock)
            {
                using (sw = new StreamWriter(fileFullFileName, true, Encoding.Default))
                {
                    sw.WriteLine(DateTime.Now.ToString(text + "," +"yyyy-MM-dd HH:mm:ss"));
                }
            }
        }


    }
}
